import { Element } from 'diagram-js/lib/model/Types';
import { ModdleElement } from 'bpmn-moddle';
import modelerStore from '@wf/components/bpmnjs/store/modeler';
import editor from '@wf/components/bpmnjs/store/editor';
import { createModdleElement, getExtensionElementsList, addExtensionElements, removeExtensionElements } from '@wf/components/bpmnjs/utils/BpmnExtensionElementsUtil';

// 获取调用接口任务配置节点对象
function getApiServiceTaskField(element: Element) {
  const prefix = editor().getProcessEngine;
  const businessObject = element.businessObject;
  const list = getExtensionElementsList(businessObject, `${prefix}:Field`);
  if (!list || !list.length) {
    return undefined;
  } else {
    const serviceTaskField = list.find((f) => f.name == 'api');
    return serviceTaskField;
  }
}

// 获取调用接口任务配置信息
export function getApiServiceTaskValue(element: Element) {
  const serviceTaskField = getApiServiceTaskField(element);
  if (serviceTaskField) {
    return JSON.parse(serviceTaskField.get('string'));
  } else {
    return undefined;
  }
}

// 设置调用接口任务配置信息
export function setApiServiceTaskValue(element: Element, object: any) {
  // 先查询是否已有，若已有则先删除
  const serviceTaskField = getApiServiceTaskField(element);
  if (serviceTaskField) {
    removeExtensionElements(element, element.businessObject, serviceTaskField);
  }
  const prefix = editor().getProcessEngine;
  const serviceTaskJson = JSON.stringify(object);
  const serviceTaskFieldElement = createModdleElement(`${prefix}:Field`, { name: 'api', string: serviceTaskJson });
  console.log('serviceTaskFieldElement', serviceTaskFieldElement);
  addExtensionElements(element, element.businessObject, serviceTaskFieldElement);
}

// 获取变量设置任务配置节点对象
function getVariableSetTaskField(element: Element) {
  const prefix = editor().getProcessEngine;
  const businessObject = element.businessObject;
  const list = getExtensionElementsList(businessObject, `${prefix}:Field`);
  if (!list || !list.length) {
    return undefined;
  } else {
    const variableSetField = list.find((f) => f.name == 'vset');
    return variableSetField;
  }
}

// 获取调用接口任务配置信息
export function getVariableSetTaskValue(element: Element) {
  const variableSetField = getVariableSetTaskField(element);
  if (variableSetField) {
    return JSON.parse(variableSetField.get('string')).variables;
  } else {
    return [];
  }
}

// 设置调用接口任务配置信息
export function setVariableSetTaskValue(element: Element, object: any) {
  // 先查询是否已有，若已有则先删除
  const variableSetField = getVariableSetTaskField(element);
  if (variableSetField) {
    removeExtensionElements(element, element.businessObject, variableSetField);
  }
  const prefix = editor().getProcessEngine;
  const variableSetJson = JSON.stringify({ variables: object });
  const variableSetFieldElement = createModdleElement(`${prefix}:Field`, { name: 'vset', string: variableSetJson });
  console.log('variableSetFieldElement', variableSetFieldElement);
  addExtensionElements(element, element.businessObject, variableSetFieldElement);
}

// 获取变量输出任务配置节点对象
function getVariablePrintTaskField(element: Element) {
  const prefix = editor().getProcessEngine;
  const businessObject = element.businessObject;
  const list = getExtensionElementsList(businessObject, `${prefix}:Field`);
  if (!list || !list.length) {
    return undefined;
  } else {
    const variablePrintField = list.find((f) => f.name == 'vprint');
    return variablePrintField;
  }
}

// 获取调用接口任务配置信息
export function getVariablePrintTaskValue(element: Element) {
  const variablePrintField = getVariablePrintTaskField(element);
  if (variablePrintField) {
    return JSON.parse(variablePrintField.get('string'));
  } else {
    return {};
  }
}

// 设置调用接口任务配置信息
export function setVariablePrintTaskValue(element: Element, object: any) {
  // 先查询是否已有，若已有则先删除
  const variablePrintField = getVariablePrintTaskField(element);
  if (variablePrintField) {
    removeExtensionElements(element, element.businessObject, variablePrintField);
  }
  const prefix = editor().getProcessEngine;
  const variablePrintJson = JSON.stringify(object);
  const variablePrintFieldElement = createModdleElement(`${prefix}:Field`, { name: 'vprint', string: variablePrintJson });
  console.log('variablePrintFieldElement', variablePrintFieldElement);
  addExtensionElements(element, element.businessObject, variablePrintFieldElement);
}
